package czk.concrent.threadSafeCollection.currenthashMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author:ChenZhangKun
 * @Date: 2020/7/26 23:44
 */
public class CurrentHashMapDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ConcurrentHashMap<Integer,Integer> map=new ConcurrentHashMap<>();
      //  HashMap<Integer,Integer> map=new HashMap<>();
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        List<Future<?>> list=new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            Future<?> submit = executorService.submit(() -> {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                for (int j = 0; j < 100; j++) {
                    int m = j;
                    map.putIfAbsent(m,map.size());
                }

            });
            list.add(submit);
        }
        for (Future<?> future : list) {
            future.get();
        }
        System.out.println(map);
        executorService.shutdown();
    }

}
